package net.uhuijia.appbase.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import net.uhuijia.appbase.dao.CrudDao;
import net.uhuijia.appbase.model.BaseExample;
import net.uhuijia.appbase.model.BaseModel;
import net.uhuijia.constants.Constants;
import net.uhuijia.constants.DBConst;
import net.uhuijia.constants.ResponseCode;
import net.uhuijia.utils.IdGen;
import net.uhuijia.utils.StringHelper;
import net.uhuijia.utils.UhuijiaJsonMapper;
import net.uhuijia.utils.UserSessionHelper;


/**
 * 增删改查基本类，提供单表的增删改查功能.
 * <p>
 * Company: uhuijia
 * </p>
 * 
 * @author: 许小宝
 * @date: 2018年7月24日 下午12:43:14
 * @version: V2.0
 * @param <S>
 * @param <M>
 * @param <E>
 */
public class CurdService<S extends CrudDao<M, E>, M extends BaseModel, E extends BaseExample>{
    
    @Autowired
    protected S mapper;
    @Autowired
    protected UhuijiaJsonMapper uhuijiaJsonMapper;
    // return code
    protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
    protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();
    protected static final String TOTAL = ResponseCode.TOTAL.getCode();
    protected static final String LIST = ResponseCode.LIST.getCode();
    protected static final String IS_MORE = ResponseCode.IS_MORE.getCode();

    /**
     * 查询记录总数.
     * 
     * @param example
     * @return
     * @throws
     */
    public int count(E example) {
        return mapper.countByExample(example);
    }

    /**
     * 根据example查询列表数据.
     * 
     * @param example
     * @return
     * @throws
     */
    public List<M> list(E example) {
        return mapper.selectByExample(example);
    }

    /**
     * 根据ID查询指定数据.
     * 
     * @param id
     * @return
     * @throws
     */
    public M get(String id) {
        if (StringHelper.isNotEmpty(id)) {
            return mapper.selectByPrimaryKey(id);
        }
        return null;
    }

    /**
     * 新增model数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String add(M model) {
        int code = mapper.insert(this.initDefalutFields(model));
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据ID更新指定数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String edit(M model) {
        int code = 0;
        String id = model.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
        	model.setUpdateTime(new Date());
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }
    
    /**
     *  编辑信息，但是不更改数据的更新时间
     * @param model
     * @return
     * @throws
     */
    public String editAndNoUpdateTime(M model) {
        int code = 0;
        String id = model.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
        	//model.setUpdateTime(new Date());
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据ID删除指定数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String delete(M model) {
        int code = 0;
        String id = model.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
        	model.setUpdateTime(new Date());
            /* if (null == model.getIsEffective() || StringHelper.isBlank(model.getIsEffective())) {
                model.setIsEffective(DBConst.FALSE);
            }*/
            model.setIsEffective(DBConst.FALSE);
            code = mapper.updateByPrimaryKeySelective(model);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

    /**
     * 根据条件批量删除
     * 
     * @param model
     * @param example
     * @return
     * @throws Exception
     */
    public String deleteByExample(M model, E example) {
        int code = 0;
        model.setUpdateTime(new Date());
        if (null == model.getIsEffective() || StringHelper.isBlank(model.getIsEffective())) {
            model.setIsEffective(DBConst.FALSE);
        }
        code = mapper.updateByExampleSelective(model, example);
        if (code > 0) {
            return SUCCESS;
        } else {
            return FAILED;
        }
    }
    
    /**
     * 默认值设置
     * 
     * @param model
     * @return
     */
	protected <W extends BaseModel> W initDefalutFields(W model) {

        if (null == model.getId() || StringHelper.isBlank(model.getId())) {
            model.setId(IdGen.uuid());
        }
        if (null == model.getCreateTime()) {
            model.setCreateTime(new Date());
        }
        if (null == model.getUpdateTime()) {
            model.setUpdateTime(new Date());
        }
        if (null == model.getCreateUser() || StringHelper.isBlank(model.getCreateUser())) {
            model.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                    .get(Constants.USER_ID) : "0");
        }
        if (null == model.getOrgCode() || StringHelper.isBlank(model.getOrgCode())) {
            model.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                    .get(Constants.USER_ORG) : "0");
        }
        if (null == model.getIsEffective() || StringHelper.isBlank(model.getIsEffective())) {
            model.setIsEffective(DBConst.TRUE);
        }
        if (null == model.getReorder() ) {
            model.setReorder(9999);
        }
        return model;
    }
}
